package cn.peng.stu4teach.util.http;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.GsonUtils;

import java.lang.reflect.Type;
import java.util.List;

/**
 * 集合型响应数据
 * 业务调用方可以参考该类对响应进行解析、包装和转换。
 */
public abstract class HttpListRe<T> implements HttpUtils.IHttpRe {
    private final Class<T> cls;

    public HttpListRe (Class<T> cls) {
        this.cls = cls;
    }

    @Override
    public void onHttpReSuccess (@NonNull String response) {
        Type listType = GsonUtils.getListType(cls);
        List<T> data = GsonUtils.fromJson(response, listType);
        if (null != data) {
            onSuccess(data);
        } else {
            onError("解析为结构体出错");
        }
    }

    @Override
    public void onHttpReError (String errorMessage) {
        onError(errorMessage);
    }

    protected abstract void onSuccess (@NonNull List<T> pReList);

    protected abstract void onError (@NonNull String pErrMsg);
}
